package com.litong.litongjiaoyu.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.litong.litongjiaoyu.annotation.IgnoreAuth;
import com.litong.litongjiaoyu.entity.SysClassschedule;
import com.litong.litongjiaoyu.entity.TeacherUserinfo;
import com.litong.litongjiaoyu.service.ClassScheduleService;
import com.litong.litongjiaoyu.service.HomeworkService;
import com.litong.litongjiaoyu.service.TeacherService;
import com.litong.litongjiaoyu.utils.JSONResult;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName TeacherController
 * @Description 教师操作类
 * @Author jingz
 * @Date 2019/7/19 0:03
 * @Version 1.0
 **/
@Log4j2
@RestController
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    @Autowired
    private HomeworkService homeworkService;

    @Autowired
    private ClassScheduleService classScheduleService;

    /***
     * @Author jingz
     * @Description 添加老师信息
     * @Date 10:06 2019/7/28
     * @Param teacherUserinfo
     * @return com.litong.litongjiaoyu.utils.JSONResult
     **/
    @IgnoreAuth
    @ResponseBody
    @RequestMapping(value = "/inserteacher", method = RequestMethod.POST)
    public JSONResult insertTeacher(@RequestBody TeacherUserinfo teacherUserinfo) {
        int ifOk = teacherService.insertTeacher(teacherUserinfo);
        if (ifOk == 1) {
            return JSONResult.ok("success");

        } else {
            return JSONResult.errorMsg("fail");
        }
    }

    /**
     * 更新信息
     *
     * @param jsonObject 前端传来的json
     * @return 是否成功
     */
    @ResponseBody
    @PostMapping(value = "/update")
    public JSONResult update(@RequestBody JSONObject jsonObject) {
        TeacherUserinfo teacherUserinfo = jsonObject.getObject("information", TeacherUserinfo.class);
        if (teacherService.upadteTeacherInfo(teacherUserinfo) == 1) {
            return JSONResult.ok("success");
        } else {
            return JSONResult.errorMsg("fail");
        }
    }

    /**
     * 根据id和phone删除所有相关信息
     *
     * @param id
     * @param phone
     * @return
     */
    @PostMapping("/remove")
    public JSONResult remove(String id, String phone) {
        int ida = Integer.parseInt(id);
        if (teacherService.remove(ida, phone) > 0) {
            return JSONResult.ok("success");
        } else {
            return JSONResult.errorMsg("fail");
        }
    }

    /**
     * 获取所有用户列表
     *
     * @return
     */
    @IgnoreAuth
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public JSONResult query() {
        List<TeacherUserinfo> userinfos = teacherService.query();
        return JSONResult.ok(userinfos);
    }


    /**
     * 员工模糊搜索
     *
     * @param text
     * @return
     */
    @IgnoreAuth
    @ResponseBody
    @PostMapping("")
    public JSONResult search(String text) {
        List<TeacherUserinfo> list = teacherService.search(text);

        if (list != null) {
            return JSONResult.ok(list);
        } else {
            return JSONResult.errorMsg("fail");
        }
    }

    /**
     * 根据id获取用户信息
     *
     * @param id
     * @return
     */
    @IgnoreAuth
    @ResponseBody
    @PostMapping("/selectInfo")
    public JSONResult selectInfo(String id) {
        int ida = Integer.parseInt(id);
        TeacherUserinfo teacherUserinfo = teacherService.selectInfo(ida);

        if (teacherUserinfo != null) {
            return JSONResult.ok(teacherUserinfo);
        } else {
            return JSONResult.errorMsg("fail");
        }
    }

    @IgnoreAuth
    @ResponseBody
    @PostMapping("/selectteacherinfo")
    public JSONResult selectTeacherInfo(String openid) {
        TeacherUserinfo teacherUserinfo = teacherService.selectTeacherInfo(openid);
        if (teacherUserinfo != null) {
            return JSONResult.ok(teacherUserinfo);
        } else {
            return JSONResult.errorMsg("fail");
        }
    }

    @ResponseBody
    @PostMapping("/batchremove")
    public JSONResult batchRemove(String[] id) {
        int r = teacherService.batchRemove(id);

        if (r > 0) {
            return JSONResult.ok("success");
        }
        return JSONResult.errorMsg("fail");
    }


    @ResponseBody
    @PostMapping("/batchposition")
    public JSONResult batchModifyPosition(int[] id, int position) {
        Map<Integer, Integer> maps = new HashMap<Integer, Integer>(16);
        for (int i = 0; i < id.length; i++) {
            maps.put(id[i], position);
        }

        int res = teacherService.batchModifyPosition(maps);
        if (res > 0) {
            return JSONResult.ok(String.format("有%d个修改成功，有%d个修改失败", res, id.length - res));
        } else {
            return JSONResult.errorMsg("修改失败！");

        }
    }

    /**
     * 添加課程
     *
     * @param sysClassscheduleList
     * @return
     */
    @ResponseBody
    @PostMapping("/addclass")
    public JSONResult addClass(@RequestBody String sysClassscheduleList) {

        JSONObject jsonObject = JSON.parseObject(sysClassscheduleList);

        JSONArray jsonArray = jsonObject.getJSONArray("sysClassscheduleList");

        List<SysClassschedule> s = JSONObject.parseArray(jsonArray.toJSONString(), SysClassschedule.class);


        return JSONResult.ok(classScheduleService.add(s));
    }

    /**
     * 更新课表
     * @param jsonObject
     * @return
     */
    @ResponseBody
    @PostMapping("/updateclass")
    public JSONResult updateClass(@RequestBody JSONObject jsonObject) {
        log.info("开始更新课表信息");
        try {
            //将获取到的列表转换成JSONArray
            JSONArray jsonArray = jsonObject.getJSONArray("sysClassscheduleList");

            //将JSONArray转化成List
            List<SysClassschedule> sysClassschedules = JSONObject.parseArray(jsonArray.toJSONString(), SysClassschedule.class);

            String campus = jsonObject.getString("campus");
            String classname = jsonObject.getString("classname");

            //先删除该校区的班级的课表
            if (classScheduleService.remove(campus, classname) > 0) {
                //重新添加已经修改过的
                if (classScheduleService.add(sysClassschedules) > 0) {
                    return JSONResult.ok();
                } else {
                    return JSONResult.errorMsg("添加失败");
                }
            } else {
                return JSONResult.errorMsg("删除失败");
            }
        } catch (Exception e) {
            log.error("更新失败，" + e.getMessage());

            return JSONResult.errorException("更新失败");
        }

    }

    /**
     * 删除某班某校区课表
     * @param jsonObject 前台传来的json
     * @return
     */
    @ResponseBody
    @PostMapping("/removeclass")
    public JSONResult removeClass(@RequestBody JSONObject jsonObject) {



        if (classScheduleService.remove(
                jsonObject.getString("campus"),
                jsonObject.getString("classname")) == 1) {
            return JSONResult.ok("success");
        } else {
            return JSONResult.errorMsg("fail");
        }
    }


    /**
     * 批量删除课表
     * @param jsonObject
     * @return
     */
    @ResponseBody
    @PostMapping("/batchremoveclass")
    public JSONResult batchRemoveClass(@RequestBody JSONObject jsonObject) {
        log.info("开始批量删除课表");

        JSONArray jsonArray = jsonObject.getJSONArray("list");


        List<SysClassschedule> sysClassschedules = JSONObject.parseArray(jsonArray.toJSONString(), SysClassschedule.class);


        if (classScheduleService.batchRemove(sysClassschedules) > 0) {
            log.info("删除成功");
            return JSONResult.ok();
        } else {
            log.info("删除失败");
            return JSONResult.errorMsg("删除失败");
        }
    }

    /**
     * 获取所有课表信息
     *
     * @return
     */
    @IgnoreAuth
    @ResponseBody
    @GetMapping("/queryclass")
    public JSONResult queryClass() {
        List<SysClassschedule> sysClassschedules = classScheduleService.query();

        return JSONResult.ok(sysClassschedules);
    }

    /**
     * 根据id获取课表信息
     *
     * @param idString
     * @return
     */
    @ResponseBody
    @PostMapping("/getclass")
    public JSONResult getClass(String idString) {
        List<SysClassschedule> sysClassschedule = classScheduleService.get(Long.parseLong(idString));

        return JSONResult.ok(sysClassschedule);
    }

    @ResponseBody
    @PostMapping("/getclassschedule")
    public JSONResult getClassSchedule(String classname, String campus) {
        return JSONResult.ok(classScheduleService.GetByClassName(classname, campus));
    }

}
